package com.yiyahui.project.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yiyahui.project.erp.domain.*;
import com.yiyahui.project.erp.domain.requestVo.ReturnVo;
import com.yiyahui.project.erp.domain.responseVo.ItemDetailVo;
import com.yiyahui.project.erp.domain.responseVo.ListMainClosingEntriesVo;
import com.yiyahui.project.erp.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.web.page.TableDataInfo;

/**
 * 主单结账记录流水Controller
 * 
 * @author yiyahui
 * @date 2020-06-10
 */
@Api("主单结账记录流水")
@RestController
@RequestMapping("/erp/entries")
public class YiyaMainClosingEntriesController extends BaseController
{
    @Autowired
    private IYiyaMainClosingEntriesService yiyaMainClosingEntriesService;

    /**
     * 查询主单结账记录流水列表
     */
    @ApiOperation("查询主单结账记录流水列表")
    @PreAuthorize("@ss.hasPermi('erp:entries:list')")
    @GetMapping("/list")
    public TableDataInfo list(YiyaMainClosingEntries yiyaMainClosingEntries)
    {
        startPage();
        List<YiyaMainClosingEntries> list = yiyaMainClosingEntriesService.selectYiyaMainClosingEntriesList(yiyaMainClosingEntries);
        return getDataTable(list);
    }

    /**
     * 导出主单结账记录流水列表
     */
    @ApiOperation("导出主单结账记录流水列表")
    @PreAuthorize("@ss.hasPermi('erp:entries:export')")
    @Log(title = "主单结账记录流水", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YiyaMainClosingEntries yiyaMainClosingEntries)
    {
        List<YiyaMainClosingEntries> list = yiyaMainClosingEntriesService.selectYiyaMainClosingEntriesList(yiyaMainClosingEntries);
        ExcelUtil<YiyaMainClosingEntries> util = new ExcelUtil<YiyaMainClosingEntries>(YiyaMainClosingEntries.class);
        return util.exportExcel(list, "entries");
    }

    /**
     * 获取主单结账记录流水详细信息
     */
    @ApiOperation("获取主单结账记录流水详细信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:entries:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yiyaMainClosingEntriesService.selectYiyaMainClosingEntriesById(id));
    }

    /**
     * 新增主单结账记录流水
     */
    @ApiOperation("新增主单结账记录流水")
    @ApiImplicitParam(name = "yiyaMainClosingEntries", value = "${tableComment}", dataType = "YiyaMainClosingEntries")
    @PreAuthorize("@ss.hasPermi('erp:entries:add')")
    @Log(title = "主单结账记录流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaMainClosingEntries yiyaMainClosingEntries)
    {
        return toAjax(yiyaMainClosingEntriesService.insertYiyaMainClosingEntries(yiyaMainClosingEntries));
    }

    /**
     * 修改主单结账记录流水
     */
    @ApiOperation("修改主单结账记录流水")
    @ApiImplicitParam(name = "yiyaMainClosingEntries", value = "${tableComment}", dataType = "YiyaMainClosingEntries")
    @PreAuthorize("@ss.hasPermi('erp:entries:edit')")
    @Log(title = "主单结账记录流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaMainClosingEntries yiyaMainClosingEntries)
    {
        return toAjax(yiyaMainClosingEntriesService.updateYiyaMainClosingEntries(yiyaMainClosingEntries));
    }

    /**
     * 删除主单结账记录流水
     */
    @ApiOperation("删除主单结账记录流水")
    @ApiImplicitParam(name = "ids", value = "主键id数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:entries:remove')")
    @Log(title = "主单结账记录流水", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaMainClosingEntriesService.deleteYiyaMainClosingEntriesByIds(ids));
    }


    /**
     * 查询主单结账记录流水列表
     */
    @ApiOperation("查询主单结账记录流水列表")
    @PostMapping("/extend/list")
    public TableDataInfo extendList(@RequestBody YiyaMainClosingEntries yiyaMainClosingEntries)
    {
        ReturnVo returnVo = yiyaMainClosingEntriesService.extendList(yiyaMainClosingEntries);
        return getDataTable(returnVo.getListOne(),returnVo.getListTwo());
    }


    /**
     * 删除主单结账记录和项目相关
     */
    @ApiOperation("删除主单结账记录流水")
    @ApiImplicitParam(name = "mainClosingEntriesId", value = "主单结账记录id", required = true)
    @Log(title = "主单结账记录流水", businessType = BusinessType.DELETE)
    @GetMapping("/delete/all")
    public AjaxResult deleteAll(Long mainClosingEntriesId)
    {
        try {
            return yiyaMainClosingEntriesService.deleteAll(mainClosingEntriesId);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }



    /**
     * 包含收费方式的主单结账记录流水列表
     */
    @ApiOperation("包含收费方式的主单结账记录流水列表")
    @PostMapping("/charge/record/list/methods")
    public ItemDetailVo methodsExtendList(@RequestBody YiyaMainClosingEntries yiyaMainClosingEntries)
    {
        ItemDetailVo itemDetailVo = new ItemDetailVo();
        ReturnVo returnVo = yiyaMainClosingEntriesService.extendList(yiyaMainClosingEntries);
        List<ListMainClosingEntriesVo> listOne = returnVo.getListOne();
        List<String> gather = listOne.stream().filter(l -> l.getYiyaClosingMethods() != null && l.getYiyaClosingMethods().size() > 0).flatMap(r -> r.getYiyaClosingMethods().stream()).collect(Collectors.toList()).stream().map(YiyaClosingMethods::getClosingMode).distinct().collect(Collectors.toList());
        itemDetailVo.setGather(gather);
        itemDetailVo.setTableDataInfo(getDataTable(listOne,returnVo.getListTwo()));
        return itemDetailVo;
    }

}
